﻿using System.Collections.Generic;
using System.Linq;
using SIG.Repository;

using SIG.Infrastructure.Logging;
using SIG.Infrastructure.Cache;
using SIG.Infrastructure.Configs;
using System.Linq.Expressions;
using System;
using SIG.Basic.Extensions;
using SIG.Model.Teams;
using SIG.Resources.Admin;

namespace SIG.Service
{
    public class TeamService: EntityService<Team>, ITeamService
    {
     
        private readonly ITeamRepository _teamRepository;
        private readonly ILoggingService _loggingService;
        private readonly ICacheService _cacheService;

        public TeamService(ITeamRepository teamRepository, ILoggingService loggingService, ICacheService cacheService)
            : base(teamRepository, cacheService, loggingService)
        {
           
            _teamRepository = teamRepository;
            _loggingService = loggingService;
            _cacheService = cacheService;
        }

        public IEnumerable<Team> GetActiveTeams()
        {
            //get list
            var key = $"{EntityNames.Team}_ActiveList";

            List<Team> teams;
            if (SettingsManager.Team.EnableCaching)
            {
                if (_cacheService.IsSet(key))
                {
                    teams = (List<Team>)_cacheService.Get(key);
                }
                else
                {
                    teams = _teamRepository.GetFilteredElements(c=>c.Active).ToList();
                    _cacheService.Set(key, teams, SettingsManager.Team.CacheDuration);
                }
            }
            else
            {
                teams = _teamRepository.GetFilteredElements(c => c.Active).ToList();
            }

            return teams;
        }


        public List<Team> GetPagedElements(int pageIndex, int pageSize, string keyword, out int totalCount)
        {

            //get list count
            var keyCount = $"{EntityNames.Team}_ListCount_{keyword}";

            var totalIQuery = _teamRepository.GetAllElements();
            if (!string.IsNullOrEmpty(keyword))
                totalIQuery = totalIQuery.Where(g => g.Name.Contains(keyword));
         

            if (SettingsManager.Team.EnableCaching)
            {
                if (_cacheService.IsSet(keyCount))
                {
                    totalCount = (int)_cacheService.Get(keyCount);
                }
                else
                {
                    totalCount = totalIQuery.Count();
                    _cacheService.Set(keyCount, totalCount, SettingsManager.Team.CacheDuration);
                }
            }
            else
            {
                totalCount = totalIQuery.Count();
            }


            //get list
            var key = $"{EntityNames.Team}_List_{pageIndex}_{pageSize}_{keyword}";

            List<Team> team;
            Expression<Func<Team, bool>> filter = g => true;
            Expression<Func<Team, bool>> filterByKeyword = g => g.Name.Contains(keyword);
         

            if (!string.IsNullOrEmpty(keyword))
                filter = filter.AndAlso(filterByKeyword);
           


            if (SettingsManager.Team.EnableCaching)
            {
                if (_cacheService.IsSet(key))
                {
                    team = (List<Team>)_cacheService.Get(key);

                }
                else
                {
                    team = _teamRepository.GetPagedElements(pageIndex, pageSize, (c => c.CreatedDate), filter, false).ToList();
                    _cacheService.Set(key, team, SettingsManager.Team.CacheDuration);
                }
            }
            else
            {
                team = _teamRepository.GetPagedElements(pageIndex, pageSize, (c => c.CreatedDate), filter, false).ToList();
            }

            return team;

        }

        public IEnumerable<Team> LatesterTeams(int count)
        {
            var key = $"{EntityNames.Team}s_Latester_{count}";

            if (!SettingsManager.Team.EnableCaching)
            {
                return _teamRepository.GetPagedElements(0, count, p => p.CreatedDate, p => p.Active, false).ToList();
            }

            if (_cacheService.IsSet(key))
            {
                return (List<Team>)_cacheService.Get(key);

            }

            var teamList = _teamRepository.GetPagedElements(0, count, p => p.CreatedDate, p => p.Active, false).ToList();
            _cacheService.Set(key, teamList, SettingsManager.Team.CacheDuration);


            return teamList;
        }
    }
}
